package me.youline.dataServer.service.front;

import java.util.ArrayList;
import java.util.List;

import me.youline.dataServer.controller.dto.PubMsgStatusDTO;
import me.youline.dataServer.entity.PubMsgStatisticRecorder;
import me.youline.dataServer.exception.UnExistDataException;
import me.youline.dataServer.service.MsgPraiseRecordService;
import me.youline.dataServer.service.PubMsgRecorderService;
import me.youline.dataServer.service.SocialRelationService;
import me.youline.dataServer.service.pubMsg.PublicMessageFacade;

import org.perf4j.aop.Profiled;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;

/**
 * 消息状态信息前端展示信息生成业务类
 * 
 * @author linhan
 *
 */
@Component
public class PubMsgStatusDTOAdapter {

	private static final Logger logger = LoggerFactory
			.getLogger(PubMsgStatusDTOAdapter.class);

	@Autowired
	public PubMsgRecorderService pubMsgRecorderService;

	@Autowired
	public PublicMessageFacade publicMessageService;

	@Autowired
	private SocialRelationService socialRelationService;

	@Autowired
	private MsgPraiseRecordService msgPraiseRecordService;

	/**
	 * 获取最新消息状态信息列表
	 * 
	 * @param viewUserId
	 * @param msgIds
	 * @return
	 */
	@Profiled(tag = "front-获取消息最新状态数据列表")
	public List<PubMsgStatusDTO> getMsgStatusDTOList(Long viewUserId,
			List<Long> msgIds) {
		if (msgIds.isEmpty()) {
			return Lists.newArrayList();
		}
		List<PubMsgStatusDTO> list = new ArrayList<PubMsgStatusDTO>(
				msgIds.size());
		for (Long msgId : msgIds) {
			try {
				list.add(this.getMsgStatusDTO(viewUserId, msgId));
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
		return list;
	}
	
	

	/**
	 * 根据请求用户ID、消息ID、状态处理上下文获取消息状态信息。它分为原创消息、转发消息两种类型分别处理
	 * 
	 * @param viewUserId
	 * @param msgId
	 * @param context
	 * @return
	 */
	@Profiled(tag = "front-获取消息最新状态数据")
	protected PubMsgStatusDTO getMsgStatusDTO(Long viewUserId, Long msgId) {
		try {
			PubMsgStatusDTO dto = null;
			dto = this.doGetStatus(viewUserId, msgId);
			dto.setMsgId(msgId);
			return dto;
		} catch (UnExistDataException e) {
			return this.doUnExistMsgProcess(viewUserId, msgId);
		}
	}

	/**
	 * 为原创消息获取其最新的状态信息
	 * 
	 * @param viewUserId
	 * @param msgId
	 * @return
	 * @throws UnExistDataException 
	 */
	protected PubMsgStatusDTO doGetStatus(Long viewUserId, Long msgId) throws UnExistDataException {
		PubMsgStatusDTO statusDTO = new PubMsgStatusDTO();
		PubMsgStatisticRecorder statisticRecorder = pubMsgRecorderService.getPubMsgStatisticRecorder(msgId);
		if(statisticRecorder == null){
			throw new UnExistDataException();
		}
		statusDTO.setPraisedNum(statisticRecorder.getPraiseNum());
		statusDTO.setIsMsgDeleted(false);
		statusDTO.setIsPraised(!msgPraiseRecordService.isAbleToPraiseMsg(msgId,
				viewUserId));
		return statusDTO;
	}


	/**
	 * 为已经不存在的消息生成一个空的状态信息
	 * 
	 * @param viewUserId
	 * @param msgId
	 * @return
	 */
	protected PubMsgStatusDTO doUnExistMsgProcess(Long viewUserId, Long msgId) {
		PubMsgStatusDTO statusDTO = new PubMsgStatusDTO();
		statusDTO.setIsPraised(false);
		statusDTO.setPraisedNum(0);
		statusDTO.setIsMsgDeleted(true);
		statusDTO.setMsgId(msgId);
		return statusDTO;
	}

}
